package com.geeke.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.geeke.common.controller.BaseController;
import com.alibaba.fastjson.JSONObject;
import com.geeke.common.data.Page;
import com.geeke.common.data.SearchParams;
import com.geeke.sys.entity.Router;
import com.geeke.sys.service.RouterService;
import com.geeke.utils.ResultUtil;

/**
 * 路由表 Controller
 */
@RestController
@RequestMapping(value = "/sys/router")
public class RouterController extends BaseController {

    @Autowired
    private RouterService routerService;

    @GetMapping("/{id}")
    public ResponseEntity<JSONObject> getById(@PathVariable("id") String id) {
        Router entity = routerService.get(id);
        return ResponseEntity.ok(ResultUtil.successJson(entity));
    }

    @PostMapping(value = { "list", "" })
    public ResponseEntity<JSONObject> listPage(@RequestBody SearchParams searchParams) {
        Page<Router> result = routerService.listPage(
            searchParams.getParams(),
            searchParams.getOffset(),
            searchParams.getLimit(),
            searchParams.getOrderby()
        );
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @PostMapping(value = "listAll")
    public ResponseEntity<JSONObject> listAll(@RequestBody SearchParams searchParams) {
        List<Router> result = routerService.listAll(searchParams.getParams(), searchParams.getOrderby());
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }
    
    @PostMapping(value = "tree")
    public ResponseEntity<JSONObject> tree(@RequestBody SearchParams searchParams) {
        List<Router> result = routerService.tree(searchParams.getParams(), searchParams.getOrderby());
        return ResponseEntity.ok(ResultUtil.successJson(result));
    }

    @PostMapping(value = "save")
    @RequiresPermissions(value={"router:create","router:update"}, logical= Logical.OR)
    public ResponseEntity<JSONObject> save(@RequestBody Router router) {
        String id = routerService.save(router).getId();
        return ResponseEntity.ok(ResultUtil.successJson(id));
    }

    @PostMapping(value = "delete")
    @RequiresPermissions("router:delete")
    public ResponseEntity<JSONObject> delete(@RequestBody Router router) {
        int rows = routerService.delete(router);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }

    @PostMapping(value = "bulkInsert")
    @RequiresPermissions("router:create")
    public ResponseEntity<JSONObject> bulkInsert(@RequestBody List<Router> routers) {
        List<String> ids = routerService.bulkInsert(routers);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    @PostMapping(value = "bulkUpdate")
    @RequiresPermissions("router:update")
    public ResponseEntity<JSONObject> bulkUpdate(@RequestBody List<Router> routers) {
        List<String> ids = routerService.bulkUpdate(routers);
        return ResponseEntity.ok(ResultUtil.successJson(ids));
    }

    @PostMapping(value = "bulkDelete")
    @RequiresPermissions("router:delete")
    public ResponseEntity<JSONObject> bulkDelete(@RequestBody List<Router> routers) {
        int rows = routerService.bulkDelete(routers);
        return ResponseEntity.ok(ResultUtil.successJson(rows));
    }
} 